

var WindTree = new Object();

/* Tree Model */

WindTreeModel = function() {
	this.nodes = new Array();
}

WindTreeModel.prototype.getNodes = function() {
	return this.nodes;
}

WindTreeModel.prototype.addNode = function(nodeModel) {
	this.nodes[this.nodes.length] = nodeModel;
}

WindTreeModel.prototype.clearNodes = function() {
	this.nodes.length = 0;
}

WindTreeModel.prototype.getNumberOfNodes = function(path) {
	var numberOfNodes = this.getNodes().length;
	if(path!=null) {
		numberOfNodes = WindTreeModel_getNumberOfNodes(this, path);
	}
	return numberOfNodes;
}

WindTreeModel.prototype.getNodeAtIndex = function(path) {
	return WindTreeModel_getNodeAtIndex(this, path); 
}

WindTreeModel.prototype.getNodeAtPath = function(path) {
	return WindTreeModel_getNodeAtPath(this, path); 
}

/* Tree Node Model */

WindTreeNodeModel = function() {
	this.nodes = new Array();
}

WindTreeNodeModel.prototype.getNodes = function() {
	return this.nodes;
}

WindTreeNodeModel.prototype.addNode = function(nodeModel) {
	this.nodes[this.nodes.length] = nodeModel;
}

WindTreeNodeModel.prototype.setView = function(view) {
	this.view = view;
}

WindTreeNodeModel.prototype.getView = function() {
	return this.view;
}

WindTreeNodeModel.prototype.getNumberOfNodes = function(path) {
	var numberOfNodes = this.getNodes().length;
	if(path!=null) {
		numberOfNodes = WindTreeModel_getNumberOfNodes(this, path);
	}
	return numberOfNodes;
}

WindTreeNodeModel.prototype.getNodeAtIndex = function(path) {
	return WindTreeModel_getNodeAtIndex(this, path); 
}

WindTreeNodeModel.prototype.getNodeAtPath = function(path) {
	return WindTreeModel_getNodeAtPath(this, path); 
}

/* Aux functions */

WindTreeModel_getNumberOfNodes = function(treeModel, path) {
	var numberOfNodes = 0;
	if(path == null) {
		numberOfNodes = treeModel.getNumberOfNodes();
	} else {
		var nodeModel = treeModel.getNodeAtPath(path);
		numberOfNodes = nodeModel.getNumberOfNodes();
	}
	return numberOfNodes;
}

WindTreeModel_getNodeAtIndex = function(treeModel, index) {
	var nodeModel = treeModel.getNodes()[index];
	return nodeModel;
}

WindTreeModel_getNodeAtPath = function(treeModel, path) {
	var nodeModel = null;
	var index = path[0];
	nodeModel = treeModel.getNodeAtIndex(index);
	if(path.length>1) {
		var _path = new Array();
		for(var i=1; i<path.length; i++) {
			_path[_path.length] = path[i];
		}
		nodeModel = nodeModel.getNodeAtPath(_path);
	}
	return nodeModel;
}

/* Create Default Model */
	
WindTree.createModel = function() {
	var model = new Object();
	
	model.getNumberOfNodes = function(path) {
		var length = 0;
		if(path==null) {
			length = 3;
		} else {
			if(path.length==1) {
				length = 3;
			} else if(path.length==2) {
				length = 3;
			}
		}
		return length;
	}

	model.getNode = function(path) {
		var nodeModel = null;
		if(path!=null) {
			if(path.length==1) {
				var label = WindLabel.createView();
				label.setText("Node " + path[0]);
				nodeModel = WindTree.createNodeModel();
				nodeModel.setView(label);
			} else if (path.length==2) {
				var label = WindLabel.createView();
				label.setText("Node " + path[0] + " " + path[1]);
				nodeModel = WindTree.createNodeModel();
				nodeModel.setView(label);
			} else if (path.length==3) {
				var label = WindLabel.createView();
				label.setText("Node " + path[0] + " " + path[1] + " " + path[2]);
				nodeModel = WindTree.createNodeModel();
				nodeModel.setView(label);
			}
		}
		return nodeModel;
	}
	
	
	model.onNodeClick = function(e, treeView, nodeView) {
	}
	
	return model;
}

/* Create View */

WindTree.createView = function(treeModel) {

	if(treeModel == null) {
		treeModel = WindTree.createModel();
	}

	var treeView = new WindView();
	treeView.setCssClass("WindTree");
	
	treeView.setModel(treeModel);
	
	var length = treeModel.getNumberOfNodes();
	var path = new Array();
	for(var i=0; i<length; i++) {
		path.length = 1;
		path[0] = i;
		var nodeView = WindTree.createNodeView(treeView, treeModel, path);
		treeView.addView(nodeView);
	}
	
	treeView.onClick = function(e) {
		e.stopPropagation();
		WindTree.setSelectedNode(treeModel, null);
		if(treeModel.onNodeClick!=null) {
			treeModel.onNodeClick(e, treeView, null);
		}
		return false;
	}
	
	if(treeModel.onContextMenu!=null) {
		treeView.onContextMenu = function(e) {
			WindTree.setSelectedNode(treeModel, null);
			return treeModel.onContextMenu(e, treeView, null);
		}
	}
	
	return treeView;
}

WindTree.updateView = function(oldView, model) {
	var newView = WindTree.createView(model);		
	newView.setId(oldView.getId());
	WindWindow.updateView(oldView, newView);
}

WindTree.createNodeView = function(treeView, treeModel, path) {
	var nodeView = new WindView();
	nodeView.setCssClass("WindTreeNode");

	var lineView = new WindView();
	lineView.setCssClass("WindTreeNode_Line");
	nodeView.addView(lineView);

	var _path = new Array();
	for(var i=0; i<path.length; i++) {
		_path[_path.length] = path[i];
		
		var indentView = new WindView();
		indentView.setCssClass("WindTreeNodeIndent");
		lineView.addView(indentView);
	}	
	
	nodeView.setPath(_path); 

	var controlsView = new WindView();
	controlsView.setCssClass("WindTreeNodeControls");
	lineView.addView(controlsView);
	
	var contentView = new WindView();
	contentView.setCssClass("WindTreeNodeContent");
	lineView.addView(contentView);

	lineView.onClick = function(e) {
		e.stopPropagation();
		WindTree.setSelectedNode(treeModel, lineView);
		return false;
	}

	if(treeModel.onNodeDblClick!=null) {
		contentView.onDblClick = function() {
			return treeModel.onNodeDblClick(path);
		}
	}

	if(treeModel.onContextMenu!=null) {
		contentView.onContextMenu = function(e) {
			WindTree.setSelectedNode(treeModel, lineView);
			return treeModel.onContextMenu(e, treeView, nodeView);
		}
	}

	var nodeModel = treeModel.getNodeAtPath(path);
	var customContentView = nodeModel.getView();
	if(customContentView!=null) {
		contentView.addView(customContentView);
	}
	
	var childNodesView = new WindView();
	childNodesView.setCssClass("WindTreeNode_Children");
	nodeView.addView(childNodesView);
	
	var length = treeModel.getNumberOfNodes(path);
	for(var i=0; i<length; i++) {
		path[path.length] = i;
		var childNodeView = WindTree.createNodeView(treeView, treeModel, path);
		path.length = path.length - 1;
		childNodesView.addView(childNodeView);
	}
	
	return nodeView;
}

WindTree.setSelectedNode = function(model, lineView) {
	var selectedNode = model.selectedNode;
	if(selectedNode!=null) {
		selectedNode.getHtmlElement().className = "WindTreeNode_Line";
	}
	if(lineView!=null) {
		model.selectedNode = lineView;
		lineView.getHtmlElement().className = "WindTreeNode_LineSelected";
	}
}

